package com.ujn.zhxy.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujn.zhxy.mapper.AdminMapper;
import com.ujn.zhxy.mapper.TeacherMapper;
import com.ujn.zhxy.pojo.Admin;
import com.ujn.zhxy.pojo.LoginFrom;
import com.ujn.zhxy.pojo.Teacher;
import com.ujn.zhxy.service.AdminService;
import com.ujn.zhxy.service.TeacherService;
import com.ujn.zhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service("teacherServiceImpl") //括号内为id
@Transactional //事务控制
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Override
    public Teacher login(LoginFrom loginFrom) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",loginFrom.getUsername())
                .eq("password", MD5.encrypt(loginFrom.getPassword()));
        Teacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    @Override
    public Teacher getTeacherById(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",userId);

        Teacher teacher = baseMapper.selectOne(queryWrapper);
        return teacher;
    }

    @Override
    public IPage<Teacher> getTeacherByOpr(
            Page<Teacher> pageParam,
            Teacher teacher
        ) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(teacher.getName())) {
            queryWrapper.like("name",teacher.getName());
        }
        if (!StringUtils.isEmpty(teacher.getClazzName())) {
            queryWrapper.like("clazz_name",teacher.getClazzName());
        }

        queryWrapper.orderByDesc("id");
        Page<Teacher> page = baseMapper.selectPage(pageParam, queryWrapper);

        return page;

    }
    //ServiceImpl<M,T>包含了针对于IService的实现
}
